= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A u t h o r :   Y o u n g w o o k   K i m   ( K o r e a n ) 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 C o n t a c t :   r u m i a 0 6 0 1 @ g m a i l . c o m 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 B a s i c   I N P U T 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 - N e w   I n p u t   i s   n e w   E v e n t 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 U s u a l l y ,   w e   l e a r n   h o w   t o   o u t p u t   s o m e t h i n g   f i r s t   ( T h i n k   a b o u t   H e l l o   W o r l d ! ) ,   l e a r n i n g   h o w   t o   i n p u t   s o m e t h i n g   i s   a l w a y s   s e c o n d .   W h y ?   B e c a u s e   i n p u t   i s   n o t   t h e   r e q u i r e m e n t   f o r   s o m e   p r o g r a m   i n   c o n t r a s t   t o   o u t p u t   i s   t h e   r e q u i r e m e n t   f o r   e v e r y   p r o g r a m .   ( T h a t  s   d e f i n i t i o n   o f   p r o g r a m .   m o r e   t h a n   o r   s a m e   a s   0   i n p u t ,   m o r e   t h a n   o r   s a m e   a s   1   o u t p u t . )   H o w e v e r ,   e v e r y   g a m e   n e e d s   i n p u t .   T h a t  s   w h y   w e   s a i d    I   l i k e   p l a y i n g   g a m e s  .   P l a y i n g   m e a n s   m o v i n g   y o u r   p a r t   o f   b o d y   ( m a y b e   y o u r   f i n g e r ) .   A n y w a y ,   l e t  s   a d d   i n p u t   l o g i c   t o   m a k e   t h i s   p r o j e c t   i n t o   r e a l   g a m e . 
 
 
 
 
 
 . .   i m a g e : :   B a g i c - I N P U T - s o u r c e c o d e . p n g 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   2 2 0 ,   1 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " B a g i c - I N P U T - s o u r c e c o d e . p n g " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 
 
 . .   i m a g e : :   B a g i c - I N P U T - r e s u l t s c r e e n . p n g 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   2 2 0 ,   1 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " B a g i c - I N P U T - r e s u l t s c r e e n . p n g " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 
 
 ( S o u r c e   C o d e   f o r   C o n t r o l l i n g   W o r l d   P r o j e c t   a n d   i t s   r e s u l t   s c r e e n ) 
 
 
 
 ( N o t   t h e   e n t i r e   s o u r c e   c o d e   o f   C o n t r o l l i n g   W o r l d   P r o j e c t ,   b u t   p a r t ) 
 
 
 
 ( C o n t r o l l i n g   W o r l d !   m o v e s   w h e n   p l a y e r   p r e s s   o n e   o f   f o u r   d i r e c t i o n   a r r o w   o f   k e y b o a r d ) 
 
 
 
 
 
 T h e r e   a r e   2   b i g   d i f f e r e n c e   i n   c o m p a r i s o n   t o   b e f o r e   p r o j e c t .   F i r s t   b i g   d i f f e r e n c e   i s   l i n e   # 5 ,   w h i c h   a d d s   c h e c k i n g   ` ` K E Y D O W N ` `   * * e v e n t * *   i s   t r i g g e r e d   o r   n o t .   O t h e r   l i n e s   a r e   j u s t   c h a n g i n g   p r e v i o u s   a l g o r i t h m   t o   a c t   d i f f e r e n t l y .   W e   k n o w   t h a t   s a m e   c o m m a n d   c a n   m a k e   b i g   d i f f e r e n c e   i n   e n t i r e   p r o g r a m   w h e n   i t   i s   e x e c u t e d   b e f o r e   E v e n t   s t a t e m e n t   o f   a f t e r   E v e n t   s t a t e m e n t .   P a y   a t t e n t i o n   t h a t   p r o c e s s   a b o u t   c h a n g i n g   l o c a t i o n   a p p e a r   a f t e r   E v e n t   s t a t e m e n t .   ( * * U p d a t e   a f t e r   s e t * * .   T h a t   i s   s e c o n d   b i g   d i f f e r e n c e ) .   V a r i a b l e   ` ` e v e n t . k e y ` `   m e a n s   l a t e s t   p r e s s e d   k e y   o n   k e y b o a r d .   L o o k   a t   t h e   s p e c i f i c   k e y   n a m e .   K _ U P ,   K _ L E F T ,   K _ D O W N ,   K _ R I G H T .   V e r y   i n t u i t i v e   * * K _   s e r i e s * * .   ( G i v e n   b y   p y g a m e . l o c a l s   w h i c h   w e   a d d e d   a t   t h e   H e a d e r )   F u r t h e r m o r e ,   t h e r e   a r e   o t h e r   k e y   n a m e d   K _ 8 ,   K _ a ,   K _ L ,   K _ L C T R L ,   K _ D E L E T E ,   o r   K _ F 4 .   W e   c a n   u n d e r s t a n d   m e a n i n g   o f   t h e s e   k e y s   w i t h o u t   e x t r a   e x p l a n a t i o n .   F u l l   k e y   l i s t   c a n   b e   f o u n d   i n     
 
 ` h t t p s : / / w w w . p y g a m e . o r g / d o c s / r e f / k e y . h t m l # p y g a m e . k e y . n a m e . ` 
 
 
 
 N o t i c e   t h a t   K E Y D O W N   m e a n s    t h i s   k e y   w a s   n o t   p r e s s e d   b e f o r e ,   b u t   * * n o w   i s   p r e s s e d * *    a n d   m e a n i n g   o f   * *  h o l d    i s   n o t   i n c l u d e d * *   h e r e .   I n   t h e   c a s e   o f   h o l d ,   n e w   e v e n t - h a n d l i n g   a b o u t   c h e c k i n g   ` ` K E Y U P ` `   ( i t   m e a n s    t h i s   k e y   w a s   p r e s s e d   b e f o r e ,   b u t   n o w   i s   n o t   p r e s s e d  )   i s   n e e d e d   w i t h   s o m e   p r o c e s s i n g   ( w h i c h   n e e d s   e x t r a   v a r i a b l e   a n d   a l g o r i t h m ) .   T h i s   w i l l   b e   m e n t i o n e d   a t   a d v a n c e d   p a r t . 
 
 
 
 A d d i n g   i n p u t   w a s   e a s y   b e c a u s e   i t  s   j u s t   a d d i n g   i f   p h a s e   w i t h   c e r t a i n   e v e n t   p a r a m e t e r .   N o w   g a m e   p r o j e c t   i s   d o n e   b e c a u s e   p r o j e c t   h a s   o u t p u t ,   p r o c e s s   a n d   i n p u t   s t e p - b y - s t e p !   R e a l l y ?   N o .   T h i s   p r o j e c t   c a n  t   b e   c a l l e d   a s   a   g a m e   b e c a u s e   t h e r e   i s   n o   * * i n t e r a c t i o n * *   b e t w e e n   a t   l e a s t   t w o   * * g a m e   o b j e c t s * * ,   n o   * * r u l e * *   f o r   p l a y i n g   t h i s   ( n e i t h e r   c o n s t r a i n s   ( e x .   H P ,   t i m e )   n o r   s c o r e ) .   M o s t l y ,   n o t   * * e n j o y a b l e * *   ( n o   m o t i v a t i o n ,   n o   v a r i e t y   o f   i n p u t   a n d   o u t p u t ,   n o   a t t r a c t i v e   c o n t e n t s )   F i r s t l y ,   w e   h a v e   t o   l e a r n   m o r e   a d v a n c e d   i n p u t   ( f o r   e x a m p l e ,   h a n d l i n g   f o r   m o u s e   s t a t u s ) ,   p r o c e s s   ( f o r   e x a m p l e ,   i d e a   f o r   f u n c t i o n a l i z a t i o n )   a n d   o u t p u t   ( f o r   e x a m p l e ,   p r i n t i n g   i m a g e / s o u n d   o r   v i s u a l i z i n g   i n t e r n a l   d a t a )   t o   f l o u r i s h   g a m e   i n t e r f a c e / s y s t e m .   D o n  t   s t a y   i n   t h e   l e v e l   o f   p r i n t i n g   c o n s t   t e x t   o r   i n p u t t i n g   s i n g l e   p r e s s e d   k e y .   O f   c o u r s e ,   e x p e r i e n c e s   o f   t h i s   l e v e l   a r e   s u r e l y   h e l p f u l   f o r   a d v a n c e d   l e v e l .   S o ,   l e t  s   g o   t o   a d v a n c e d   l e v e l ! 
 
 
 
 
 
 
 
 < R e f e r e n c e   C o d e >   : : 
 
 
 
         i m p o r t   p y g a m e ,   s y s 
 
         f r o m   p y g a m e . l o c a l s   i m p o r t * 
 
 
 
         w h i t e   =   ( 2 5 5 , 2 5 5 , 2 5 5 ) 
 
         r e d   =   ( 2 5 5 , 0 , 0 ) 
 
         g r e e n   =   ( 0 , 2 5 5 , 0 ) 
 
         p y g a m e . i n i t ( ) 
 
         p y g a m e . d i s p l a y . s e t _ c a p t i o n ( " C o n t r o l l i n g   W o r l d   P r o j e c t " )   
 
         m y S c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( ( 6 4 0 ,   4 8 0 ) ) 
 
         m y T e x t F o n t   =   p y g a m e . f o n t . F o n t ( " H o o n W h i t e c a t R . t t f " ,   3 2 ) 
 
         m y T e x t   =   m y T e x t F o n t . r e n d e r ( " C o n t r o l l i n g   W o r l d ! " ,   T r u e ,   r e d ,   g r e e n )   
 
         m y T e x t A r e a   =   m y T e x t . g e t _ r e c t ( ) 
 
         m y T e x t A r e a . c e n t e r   =   ( 3 2 0 ,   2 4 0 ) 
 
         f p s C l o c k   =   p y g a m e . t i m e . C l o c k ( ) 
 
         x   =   0 
 
         y   =   0 
 
         m o v e R i g h t   =   0   # 1 
 
         m o v e D o w n   =   0   # 2 
 
 
 
         w h i l e   T r u e : 
 
                 m o v e R i g h t   =   0   # 3 
 
                 m o v e D o w n   =   0   # 4 
 
                 m y T e x t A r e a . c e n t e r   =   ( 3 2 0   +   x ,   2 4 0   +   y ) 
 
                 m y S c r e e n . f i l l ( w h i t e ) 
 
                 m y S c r e e n . b l i t ( m y T e x t ,   m y T e x t A r e a ) 
 
 
 
                 f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                         i f   e v e n t . t y p e   = =   Q U I T : 
 
                                 p y g a m e . q u i t ( ) 
 
                                 s y s . e x i t ( ) 
 
                         e l i f   e v e n t . t y p e   = =   K E Y D O W N :   # 5 
 
                                 i f   e v e n t . k e y   = =   K _ U P : 
 
                                         m o v e D o w n   =   - 1 
 
                                         m o v e R i g h t   =   0 
 
                                 e l i f   e v e n t . k e y   = =   K _ L E F T : 
 
                                         m o v e D o w n   =   0 
 
                                         m o v e R i g h t   =   - 1 
 
                                 e l i f   e v e n t . k e y   = =   K _ D O W N : 
 
                                         m o v e D o w n   =   1 
 
                                         m o v e R i g h t   =   0 
 
                                 e l i f   e v e n t . k e y   = =   K _ R I G H T : 
 
                                         m o v e D o w n   =   0 
 
                                         m o v e R i g h t   =   1 
 
                                 
 
                 i f ( m o v e R i g h t   = =   1 ) :   # 6 
 
                         x   =   x   +   1 0 
 
                 e l i f ( m o v e R i g h t   = =   - 1 ) :   # 7 
 
                         x   =   x   -   1 0 
 
                 i f ( m o v e D o w n   = =   1 ) :   # 8 
 
                         y   =   y   +   1 0 
 
                 e l i f ( m o v e D o w n   = =   - 1 ) :   # 9 
 
                         y   =   y   -   1 0 
 
 
 
                 p y g a m e . d i s p l a y . u p d a t e ( ) 
 
 
 
 